package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Validated
@RestController
@Tag(name = "02-车辆管理模块")
@RequestMapping("/v1/vehicle")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 1.添加车辆功能
     */
    @PostMapping("/add")
    @Operation(summary = "添加车辆功能")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session){
        log.debug("添加车辆业务参数: " + vehicleAddParam);
        /*
            1.校验用户登录状态;
            2.未登录: 直接返回;
            3.已登录: 添加车辆[INSERT接口];
         */
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录状态校验已经在拦截器中实现
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());
        log.debug("实体类:" + vehicle);
        int i = vehicleMapper.insertVehicle(vehicle);

        if (i > 0){
            return JsonResult.ok();
        }

        return new JsonResult(StatusCode.UKNOWN_ERROP);
    }

    /**
     * 2.查询车辆列表功能
     */
    @GetMapping("/list")
    @Operation(summary = "查询车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {
            @Parameter(name = "brand"),
            @Parameter(name = "license"),
            @Parameter(name = "vehicleListQuery", hidden = true),
    })
    public JsonResult listVehicle(VehicleListQuery vehicleListQuery, HttpSession session){
        log.debug("查询车辆列表业务参数: " + vehicleListQuery);
        //校验用户登录状态
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录状态校验已经在拦截器中实现
        //已登录
        /*
            1.未给参数；
            2.只给brand参数情况;
            3.只给license参数情况;
            4.给brand和license参数情况;
            SELECT ... FROM vehicle WHERE brand like '%brand%' AND license like '%license%' AND user_id=?;
         */
        Long userId = userVO.getId();
        List<VehicleListVO> vehicleListVOS = vehicleMapper.listVehicle(vehicleListQuery, userId);

        return JsonResult.ok(vehicleListVOS);
    }

    /**
     * 3.删除车辆功能[伪删除]
     */
    @PostMapping("/del/{id}")
    @Operation(summary = "删除车辆功能")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteVehicle(@PathVariable @Range(min = 1L, max = 500L, message = "车辆id必须在1-500之间") Long id, HttpSession session){
        //手动抛出异常,用于演示全局异常处理器的使用;
        if (id < 0){
            //选择1: throw new IllegalArgumentException("车辆id不能小于0");
            throw new IllegalArgumentException("车辆id不能小于0");
            //选择2: return new JsonResult(StatusCode.OPERATION_FAILED, "车辆id不能小于0");
        }

        log.debug("删除车辆业务参数: " + id);
        //1.校验登录状态;
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录状态校验已经在拦截器中实现
        Long userId = userVO.getId();
        int i = vehicleMapper.deleteVehicle(id, userId);
        if (i > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
}









